package pl.un4giv3n.mightvsmagic.action;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.entity.Message;
import pl.un4giv3n.mightvsmagic.entity.User;

public class DeleteMessageAction extends PersistenceAction{

	private static final long serialVersionUID = 1L;

	private long messageId;
	
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		User user = getSessionUser(entityManager);
		if(user == null){
			entityManager.close();
			return LOGIN;
		}
		Message message = entityManager.find(Message.class, messageId);
		if(message == null){
			addActionError("Nie znaleziono wiadomości");
			entityManager.close();
			return INPUT;
		}
		entityManager.getTransaction().begin();
		entityManager.remove(message);
		entityManager.flush();
		entityManager.getTransaction().commit();
		entityManager.close();
		return SUCCESS;
	}

	public long getMessageId() {
		return messageId;
	}

	public void setMessageId(long messageId) {
		this.messageId = messageId;
	}
}
